package com.party.service.lodge.impl;

import com.party.common.Page;
import com.party.mapper.LodgeMapper;
import com.party.model.Lodge;
import com.party.model.LodgeExample;
import com.party.service.info.InfoService;
import com.party.service.lodge.LodgeService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-9-18
 * Time: 下午10:38
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class LodgeServiceImpl implements LodgeService {

    @Resource
    LodgeMapper lodgeMapper;

    @Override
    public int addLodge(Lodge lodge) {
        int num = lodgeMapper.insertSelective(lodge);
        return num;
    }

    @Override
    public int deleteLodgeByPrimaryKey(Integer id) {
        int num = lodgeMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public int deleteLodgeByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += lodgeMapper.deleteByPrimaryKey(Integer.parseInt(roid));
            }
        }
        return num;
    }

    @Override
    public Lodge getLodgeByPrimaryKey(Integer id) {
        Lodge lodge = lodgeMapper.selectByPrimaryKey(id);
        return lodge;
    }

    @Override
    public int updateLodgeByPrimaryKey(Lodge lodge) {
        int num = lodgeMapper.updateByPrimaryKeySelective(lodge);
        return num;
    }

    @Override
    public Page getLodgeList(Lodge lodge, int pageSize, int pageNumber) {
        LodgeExample lodgeExample = new LodgeExample();
        /**
         * 可以添加查询条件
         */
        if(lodge != null)
        {
            LodgeExample.Criteria criteria = lodgeExample.createCriteria();
            if(lodge.getTitle() != null && !"".equals(lodge.getTitle()))
            {
                criteria.andTitleLike("%"+lodge.getTitle()+"%");
            }

            if(lodge.getAuthor() != null && !"".equals(lodge.getAuthor()))
            {
                criteria.andAuthorLike("%" + lodge.getAuthor() + "%");
            }

            if(lodge.getCode() != null && !"".equals(lodge.getCode()))
            {
                criteria.andCodeLike("%" + lodge.getCode() + "%");
            }
        }
        lodgeExample.setOrderByClause("time desc");

        //获取总的记录数量
        int count = lodgeMapper.countByExample(lodgeExample);
        //获取指定的页码数据
        lodgeExample.setBeginNum((pageNumber - 1) * pageSize);
        lodgeExample.setEndNum(pageSize);
        List<Lodge> list = lodgeMapper.getListPage(lodgeExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }

    @Override
    public List<Lodge> getAllLodge() {
        LodgeExample lodgeExample = new LodgeExample();
        List<Lodge> list=this.lodgeMapper.getAllLodge(lodgeExample);
        return list;
    }
}
